<?php
namespace App\Http\Controllers\AppApi\My;

use App\Models\ShopAddressModel;
use App\Services\Admin\JobStaffService;
use App\Services\Admin\UserService;
use App\Utilities\Upload;
use Illuminate\Http\Request;
use Hash;

class ShopAddressController extends BaseController
{
    protected $user;
    protected $job;

    public function __construct(Request $request)
    {
        parent::__construct();
        $this->user = new UserService();
        $this->job = new JobStaffService();

        if ($request->app_access!=$this->app_access) {
            return $this->returnJson(-1,'接口权限错误');
        }
        if (!$request->token) {
            return $this->returnJson(-2,'参数错误');
        }

        $this->uid = $this->accessGet($request->token);
        if(!$this->uid){
            return $this->returnJson(-13,'您未登录或未注册');
        }

    }

    /**
     * 获取用户收货地址
     */
    public function getAreaList()
    {
        $addressinfo = ShopAddressModel::where([ 'uid' => $this->uid ])
            ->where('users_address.status', '!=', 0)
            ->select('users_address.id','users_address.uid','users_address.name','users_address.phone','users_address.address','users_address.status', 'p.name as provincename', 'c.name as cityname', 'x.name as xianname')
            ->join('area as p', 'users_address.province_id', '=', 'p.id')
            ->join('area as c', 'users_address.city_id', '=', 'c.id')
            ->join('area as x', 'users_address.xian_id', '=', 'x.id')
            ->orderBy('users_address.status', 'desc')
            ->orderBy('id', 'desc')
            ->get();
        $addressinfo = $addressinfo->isEmpty() ? [] : $addressinfo->toArray();
        return $this->toJson(0,$addressinfo);
    }

    /**
     * 新增收货地址
     */
    public function addArea(Request $request)
    {
        $province_id = intval($request->input('province_id',0));
        $city_id = intval($request->input('city_id',0));
        $xian_id = intval($request->input('xian_id',0));
        $name = trim($request->input('name',''));
        $phone = trim($request->input('phone',''));
        $address = trim($request->input('address',''));
        $status = intval($request->input('status',1));
        if(!$province_id || !$city_id || !$xian_id || !$name || !$phone || !$address){
            return $this->returnJson(-2,'参数错误');
        }
        $dataArr = array(
            'uid' => $this->uid,
            'province_id' => $province_id,
            'city_id' => $city_id,
            'xian_id' => $xian_id,
            'name' => $name,
            'phone' => $phone,
            'address' => $address,
            'status' => $status,
        );

        if(ShopAddressModel::insert($dataArr)){
            return $this->toJson(0);
        }else{
            return $this->toJson(-100);
        }
    }


    /**
     * 修改收货地址
     */
    public function editArea(Request $request)
    {
        $id = $request->input('id',0);
        if(!$id){
            return $this->toJson(-2);
        }
        $address = ShopAddressModel::where('id',$id)->first();
        if(!$address){
            return $this->toJson(-3);
        }
        if(!$address->status || $address->uid != $this->uid){
            return $this->toJson(-99);
        }

        $updateData = [];
        $province_id = intval($request->input('province_id',0));
        if($province_id){
            $updateData['province_id']=$province_id;
        }
        $city_id = intval($request->input('city_id',0));
        if($city_id){
            $updateData['city_id']=$province_id;
        }
        $xian_id = intval($request->input('xian_id',0));
        if($xian_id){
            $updateData['xian_id']=$province_id;
        }
        $name = trim($request->input('name',0));
        if($name){
            $updateData['name']=$province_id;
        }
        $phone = trim($request->input('phone',0));
        if($phone){
            $updateData['phone']=$province_id;
        }
        $address = trim($request->input('address',0));
        if($address){
            $updateData['address']=$province_id;
        }
        $status = intval($request->input('status',0));
        if($status){
            $updateData['status']=$province_id;
        }

        if(ShopAddressModel::where('id',$id)->update($updateData)){

            ShopAddressModel::where('id', '!=', $id)->where([ 'uid' => $this->uid, 'status' => 2 ])->update(['status'=>1]);

            return $this->toJson(0);
        }else{
            return $this->toJson(-100);
        }
    }

    /**
     * 删除收货地址
     */
    public function delArea(Request $request)
    {
        $id = $request->input('id',0);
        if(!$id){
            return $this->toJson(-2);
        }

        $address = ShopAddressModel::where('id',$id)->first();
        if(!$address){
            return $this->toJson(-3);
        }
        if(!$address->status){
            return $this->toJson(0);
        }
        if($address->uid != $this->uid){
            return $this->toJson(-99);
        }

        $address->status = 0;
        if($address->save()){
            return $this->toJson(0);
        }else{
            return $this->toJson(-100);
        }
    }



}